package com.qf.smsplatform.webmaster.controller;

import com.qf.constant.RabbitMQConstants;
import com.qf.constant.ReportStateConstants;
import com.qf.constant.SourceType;
import com.qf.model.StandardSubmit;
import com.qf.smsplatform.webmaster.config.IdWorker;
import com.qf.smsplatform.webmaster.dto.SmsDTO;
import com.qf.smsplatform.webmaster.pojo.TAdminUser;
import com.qf.smsplatform.webmaster.util.R;
import com.qf.smsplatform.webmaster.util.ShiroUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Controller
public class SmsController {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    private IdWorker idWorker;

    @ResponseBody
    @RequestMapping("/sys/sms/save")
    public R addBlack(@RequestBody SmsDTO smsDTO){
        //TODO===========================================================

        TAdminUser userEntity = ShiroUtils.getUserEntity();
        Integer clientid = userEntity.getClientid();
        String mobile = smsDTO.getMobile();
        String[] split = mobile.split("\n");
        for (String s : split) {
           //  StandardSubmit standardSubmit = new StandardSubmit();
           //  standardSubmit.setClientID(clientid);
           //  standardSubmit.setDestMobile(s);
           //  standardSubmit.setMessageContent(smsDTO.getContent());
           //  standardSubmit.setSource(2);//因为是通过网页发送的,所以不需要接收状态报告,所以设置 2
           //  standardSubmit.setSendTime(new Date());
           // //TODO 这里短信对象的sequenceid没有，在gateway模块会报错
           //  standardSubmit.setSrcSequenceId(userEntity.getClientid().longValue());
           //  rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PRE_SEND, standardSubmit);

            //TODO===========================================================
            StandardSubmit standardSubmit = new StandardSubmit();
            //当前StandardSubmit唯一id
            standardSubmit.setSequenceId(String.valueOf(idWorker.nextId()));
            //客户端id
            standardSubmit.setClientID(clientid);
            //消息的优先级 0最低 --- 3最高
            //standardSubmit.setMessagePriority(Short.parseShort(client.get("priority")));
            //客户端唯一序列号
            standardSubmit.setSrcSequenceId(userEntity.getClientid().longValue());
            //目的手机号
            standardSubmit.setDestMobile(s);
            //短信内容
            standardSubmit.setMessageContent(smsDTO.getContent());
            //手机接收的状态值 0成功 1等待 2失败
            standardSubmit.setReportState(ReportStateConstants.WAIT);
            //短信发送时间
            LocalDateTime localDateTime = LocalDateTime.now();
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date date = Date.from(instant);
            standardSubmit.setSendTime(date);
            //发送方式 1http 2web
            standardSubmit.setSource(SourceType.HTTP);
            rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PRE_SEND,standardSubmit);
        }
        return R.ok();
    }

}
